﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Domain.Entities;

namespace Aga.GestionDeArchivos.Web
{
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        public Actions Actions;

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
                throw new ArgumentNullException("httpContext");

            string[] users = Users.Split(',');

            if (!httpContext.User.Identity.IsAuthenticated)
                return false;

            var user = (User)httpContext.Session[SessionKey.LoggedInUser];

            if (user == null)
                return false;

            UserService service = new UserService();

            return service.UserCan(user.Id, Actions);
        }
    }
}